﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;

namespace PRE.Catalog
{
    public partial class FrmDanhSachSachMuon : DevExpress.XtraEditors.XtraForm
    {
        #region Variable
        DAL.EntitiesDAL.DuocMuonDAL _dal = new DAL.EntitiesDAL.DuocMuonDAL();
       
        BLL.EntitiesBLL.DuocMuonBLL _bll = new BLL.EntitiesBLL.DuocMuonBLL();

        DataTable _dt = new DataTable();

        #endregion

        #region Inits

        public FrmDanhSachSachMuon()
        {
            InitializeComponent();
        }

        #endregion

        #region Load du lieu
        private void FrmDanhSachSachMuon_Load(object sender, EventArgs e)
       {
           GetData();
       }
        #endregion

        #region thoat
        private void cbo_thoat_Click(object sender, EventArgs e)
       {
           this.Close();
       }
       #endregion

        #region Làm tươi
       private void cbo_refresh_Click(object sender, EventArgs e)
       {
           FrmDanhSachSachMuon_Load(null, null);
       }
       #endregion

        #region In dữ liệu
       private void cbo_in_Click(object sender, EventArgs e)
       {
            try
            {
                if (_dt.Select("IsCheck = True").Length == 0)
                {
                    XtraMessageBox.Show("Chưa chọn sách mượn cần in", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                string _NgayKy = "TP.Cần Thơ, ngày " + DateTime.Now.Day + " tháng " + DateTime.Now.Month + " năm " + DateTime.Now.Year;
               
                if (gridView_SachMuon.DataRowCount > 0)
                {
                    DataTable dt = gridView_SachMuon.GetDataRow(0).Table.Clone();

                    for(int i = 0; i < gridView_SachMuon.DataRowCount; i++)
                    {
                        if (gridView_SachMuon.GetDataRow(i)["IsCheck"].ToString() == "True")
                            dt.ImportRow(gridView_SachMuon.GetDataRow(i));
                    }

                    Reports.FrmReportView frm = new Reports.FrmReportView();
                    frm._load_XtraDanhSachSachMuon(dt, _NgayKy);
                    frm.Show(); 
                }
            }
            catch { }
       }
       #endregion

        #region Lọc dữ liệu
    private void cbo_loc_Click(object sender, EventArgs e)
    {
        try
        {
            DateTime FromDate =  Convert.ToDateTime(dte_tungay.Text);
            DateTime ToDate =  Convert.ToDateTime(dte_denngay.Text);

            DataTable _dtSearch = _dal.Search_FromDate_ToDate(FromDate, ToDate);

            if (_dtSearch != null)
            {
                _dtSearch.Columns.Add("IsCheck", typeof(bool));

                foreach (DataRow dr in _dtSearch.Rows)
                    dr["IsCheck"] = false;

                gridControl_SachMuon.DataSource = _dtSearch;

                AppGridView.SummaryField(gridView_SachMuon, "MACUONSACH", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);
                AppGridView.ReadOnlyColumn(gridView_SachMuon,
                    new string[] { "STT_PM", "MACUONSACH","TENDAUSACH", "NOIDUNG_TT", "LANXUATBAN", "KICHTHUOC", "SOTRANG", "TENKESACH"
                           , "TENTHELOAI", "TENNHAXB", "NGAYMUON" });

                gridView_SachMuon.OptionsView.EnableAppearanceEvenRow = true;
                gridView_SachMuon.OptionsView.ColumnAutoWidth = true;
              }
            gridControl_SachMuon.DataSource = _dtSearch;

            _dt = _dtSearch;
        }
        catch {}  
    }
    #endregion

        #region Chọn tất cả
       private void checkEdit_All_CheckedChanged(object sender, EventArgs e)
       {
           for (int i = 0; i < gridView_SachMuon.DataRowCount; i++)
           {
               gridView_SachMuon.GetDataRow(i)["IsCheck"] = checkEdit_All.Checked;
           }
       }
       #endregion

        #region GetData
       private void GetData()
       {
           try
           {
               _dt = _dal.Select_sachmuon();

               if (_dt != null)
               {
                   _dt.Columns.Add("IsCheck", typeof(bool));

                   foreach (DataRow dr in _dt.Rows)
                       dr["IsCheck"] = false;

                   gridControl_SachMuon.DataSource = _dt;

                   AppGridView.SummaryField(gridView_SachMuon, "MACUONSACH", "Tổng = {0:#,0}", DevExpress.Data.SummaryItemType.Count);
                   AppGridView.ReadOnlyColumn(gridView_SachMuon,
                       new string[] { "STT_PM", "MACUONSACH","TENDAUSACH", "NOIDUNG_TT", "LANXUATBAN", "KICHTHUOC", "SOTRANG", "TENKESACH"
                           , "TENTHELOAI", "TENNHAXB", "NGAYMUON" });

                   gridView_SachMuon.OptionsView.EnableAppearanceEvenRow = true;
                   gridView_SachMuon.OptionsView.ColumnAutoWidth = true;
               }
           }
           catch (Exception ex)
           {
               XtraMessageBox.Show("Lỗi! Không load được dữ liệu", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
           }
       }
        #endregion 
    }
}